With this technique you can make a network connection from your Windows or Mac computer to a Raspberry Pi using nothing more than a USB cable!
Sometimes you just want to run a Raspberry Pi without connecting to a screen, keyboard and mouse. You can make a remote connection from your computer to the Pi using SSH or VNC, but first you need to connect to the same network as your main computer. You can do this using WiFi or ethernet, but sometimes this is not so conveient. An alternative is to use this technique, which puts your Pi into USB gadget mode.
The config.txt file can be found in the boot directory. Edit it using sudo nano /boot/config.txt. Then add the following line:
dtoverlay=dwc2
The cmdline.txt file can be found in the boot directory. Edit it using sudo nano /boot/cmdline.txt. You will see a very long line, starting console=. You need to add the following text straight after the rootwait word:
modules-load=dwc2,g_ether
The line should look something like this when you are finished:
console=serial0,115200 console=tty1 root=PARTUUID=69a16fdd-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
modules-load=dwc2,g_ether quiet splash plymouth.ignore-serial-consoles
Mac users will have this on by default, but Windows users may need to install the Bonjour service, which allows Windows to discover devices such as printers. You can install it from here: Download Bonjour Print Services for Windows
Now plug the Pi into your computer using a USB cable. Your computer should detect it. You can SSH from a command prompt or terminal session:
ssh pi@raspberrypi.local
Replace "raspberrypi" with your Pi's network name if you have changed it.
You can also use VNC to get a desktop view of your Pi.
A more detailed explanation of this technique can be found here: Adafruit Ethernet Gadget